﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ClassManager_AddSMALLType : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

           



            RECORDID.Text = "";
            String rID = Page.Request["ID"];
            if (rID != null && rID != "")
            {
                EntityOprHelper helper = new EntityOprHelper();
                T_PRODUCT_SMALLTYPE add = helper.GetEntityByWhere<T_PRODUCT_SMALLTYPE>(" where ID=" + rID + "");
                if (add != null)
                {
                   
                    DataTable _table = helper.GetDataTableBySql("select TYPEID,NAME FROM T_TYPE");

                    pType.DataValueField = "TYPEID";
                    pType.DataTextField = "NAME";

                    pType.DataSource = _table.DefaultView;
                    pType.DataBind();

                   

                    RECORDID.Text = add.ID.ToString();
                   
                    pName.Text = add.NAME;
                    pImage.Text = add.IMAGE;

                    BindBigType(add.PRODUCT_BIGTYPEID);
                    pType.SelectedValue = getBigType(add.PRODUCT_BIGTYPEID);

                    BindBigType(pType.SelectedValue);
                    bType.SelectedValue = add.PRODUCT_BIGTYPEID;

                }
            }
            else
            {
                EntityOprHelper helper = new EntityOprHelper();
                DataTable _table = helper.GetDataTableBySql("select TYPEID,NAME FROM T_TYPE");

                pType.DataValueField = "TYPEID";
                pType.DataTextField = "NAME";

                pType.DataSource = _table.DefaultView;
                pType.DataBind();

                if (_table.Rows.Count > 0)
                {
                    BindBigType(_table.Rows[0][0].ToString());
                }
            }

        }

    }
    private String getBigType(String type)
    {
        EntityOprHelper helper = new EntityOprHelper();
        T_PRODUCT_BIGTYPE _type = helper.GetEntityByWhere<T_PRODUCT_BIGTYPE>(" WHERE PID='" + type + "'");
        return _type.TYPEID;
    }

    private void BindBigType(String pid)
    {

        EntityOprHelper helper = new EntityOprHelper();
        DataTable _table = helper.GetDataTableBySql("select PID,NAME FROM T_PRODUCT_BIGTYPE WHERE TYPEID='" + pid + "'");

        bType.DataValueField = "PID";
        bType.DataTextField = "NAME";

        bType.DataSource = _table.DefaultView;
        bType.DataBind();

    }
    protected void pType_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindBigType(pType.SelectedValue);
    }
    protected void bType_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Submit_Click(object sender, EventArgs e)
    {

        EntityOprHelper helper = new EntityOprHelper();

        if (RECORDID.Text == "")
        {
            if (pName.Text == "")
            {
                return;
            }
            T_PRODUCT_SMALLTYPE _tp = helper.GetEntityByWhere<T_PRODUCT_SMALLTYPE>(" where NAME='" + pName.Text + "';");
            if (_tp != null)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('该类型已经存在!')", true);
                return;
            }


            T_PRODUCT_SMALLTYPE type = new T_PRODUCT_SMALLTYPE();
            type.PID = System.Guid.NewGuid().ToString("N").ToUpper();
            type.NAME = pName.Text;
            type.IMAGE = pImage.Text;
            type.PRODUCT_BIGTYPEID = bType.SelectedValue;

            bool b = helper.AddEntity<T_PRODUCT_SMALLTYPE>(type);
            if (b)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('添加成功!')", true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('添加失败!')", true);
            }
        }
        else
        {
            T_PRODUCT_SMALLTYPE address = new T_PRODUCT_SMALLTYPE();
            address.NAME = pName.Text;
            address.IMAGE = pImage.Text;
            address.PRODUCT_BIGTYPEID = bType.SelectedValue;
            address.ID = Convert.ToInt32(RECORDID.Text);

            bool b1 = helper.EditEntity<T_PRODUCT_SMALLTYPE>(address);
            if (b1)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('修改成功')", true);
                pName.Text = "";
                RECORDID.Text = "";
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('修改失败')", true);
            }

            return;

        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        pImage.Text = shareCode.upFileToServer();
    }
    
   
}
